RSLondon Software Carpentry 2020-11-25/27
This collaborative document (hackmd) is synchronized as you type, so that everyone viewing this page sees the same text. This allows you to collaborate seamlessly on documents.
Users are expected to follow our code of conduct.
The Workshop webpage contains the installations instructions for the software that we will use during the next three days.
Zoom links for each day will be shared via e-mail.
Pre-workshop Questionnaire
The Unix Command line - Chris Cave-Ayland
- To prepare for the session please:
Notes (for everyone to add them together 😉)
-
ls
shows the files
ls -F
shows also a trailing slash for the directories (useful if you’ve haven’t got them already in a different colour). -F
is an “option”.
ls -F /
shows what’s under the /
directory.
ls --help
shows us how to use it! (a bit cryptic though)
[OPTION]...
You can provide options,
[]
means they are optional, the `
- …` mean that you can provide more than one option.
[FILE]...
we can ask for a location or many as we want and they are optional.
- There are short and long versions:
-a
= --all
man ls
is similar to the help output. You need to press q to exit. The output is clear out from the screen.
- Options and everything in the shell is case sensitive!
-s
and -S
are different things.
-
pwd
print working directory; first /
in the output is called the root directory, the base of all the directories.
-
cd Desktop
moves your location to the Desktop directory
-
~
is a short for /home/username
, your Home
-
Paths:
- absolute: refers from the root, eg.,
/home/user/Desktop/data-shell
- relative: refers from the point you are in, if we are in Desktop:
data-shell
..
: refers to the parent directory
.
: refers to the current directory
-
nano
open a text editor within the shell. ctrl+x to exit.
-
cp
copies (and renames if needed) something into somewhere cp quotes.txt thesis/quotations.txt
cp -r
to copy a directory
-
rm
to delete (remove) files. there’s not a recycle bin !! so be careful
rm -r
will allow you to delete directories
rm -i
will ask you for confirmation before deleting stuff.
-
wc
count characters, words and lines
-
>
is use to redirect the output of a command into a file
-
cat
to display files on the screen
-
sort
to sort a file, -n
to sort numerically.
-
head
shows the first few lines of the file, -n 1
will only show the first line.
-
tail
like head
but for the end of the file
-
|
(pipe) is use to redirect the output of one command as input for the next (wc -l *pdb | sort -n | head -n 1
will give the file with the smaller number of lines named ...pdb
)
-
for loops, to repeat an action over multiple files.
$ for filename in basilisk.dat minotaur.dat unicorn.dat
> do
> head -n 2 $filename | tail -n 1
> done
-
scripts, files that we put the commands we want to run in sequence. Normally called as some_meaningful_name.sh
.
- if used
$@
within the script, it will read all the arguments you pass from the command line.
example:
for filename in "$@"
do
head -n 15 "$filename" | tail -n 5
done
-
history
shows you all the commands you’ve run.
-
tab will try to autocomplete the files and directory names.
-
ctrl+c is used to cancel what you are running.
Questions about Unix command line
Here you can post any question you have while we are going through this document. Please, use a new bullet point for each question and sub-bullet points for their answers.
For example writing like this:
- Example question
- [name=student_a] Example answer
- [name=TA_1] Example answer
produces the following result:
-
Example question
- student_a Example answer
- TA_1 Example answer
-
What’s the difference between git bash and git cmd?
- Iain S Git CMD is just like regular Windows command prompt with the git command. Git Bash emulates a bash environment on windows. It lets you use all git features in command line plus most of standard unix commands.
-
Is the sorting (when using ls
) displayed in vertical or horizontal order?
- David it’s displayed in columns so left column first, then go through the second column.
-
What if I wanted only the name itself without the first bit of the line (CLASSIFICATION:)?
Feedback
Tell us at least one good thing and something we could improve by leaving feedback as post-its on the interactive board.
Version Control with Git - Iain Barrass
Notes
Software Carpentry lesson material for GitHub Remotes.
Questions about Git
- Example question
- student_a Example answer
- TA_1 Example answer
Feedback
Tell us at least one good thing and something we could improve by leaving feedback as post-its on the interactive board.
Python - day 1 - Iain Stenson
Notes
Download python-novice-inflammation-data.zip.
Questions about Python 1/2
- Example question
- student_a Example answer
- TA_1 Example answer
Exercises
Exercise Session 1
You can find tips or answers to most exercises in the lesson notes.
1.
What values do the variables mass and age have after each of the following statements? Test your answer by executing the lines.
mass = 47.5
age = 122
mass = mass * 2.0
age = age - 20
2.
What does the following program print out?
first, second = 'Grace', 'Hopper'
third, fourth = second, first
print(third, fourth)
Exercise Session 2
A section of an array is called a slice. We can take slices of character strings as well:
element = 'oxygen'
print('first three characters:', element[0:3])
print('last three characters:', element[3:6])
- What is the value of element[:4]? What about element[4:]? Or element[:]?
- What is element[-1]? What is element[-2]?
- Given those answers, explain what element[1:-1] does.
- How can we rewrite the slice for getting the last three characters of element, so that it works even if we assign a different string to element? Test your solution with the following strings: carpentry, clone, hi.
Exercise Session 3
Why do all of our plots stop just short of the upper end of our graph? Can we adjust the y limit of our graph so that it looks better?
some_plot.set_ylim(min, max)
Can you create your own graph of the standard deviation using
np.std()
Exercise Session 4
1.
Python has a built-in function called range
that generates a sequence of numbers. range
can accept 1, 2, or 3 parameters. If one parameter is given, range generates a sequence of that length, starting at zero and incrementing by 1. For example, range(3)
produces the numbers 0, 1, 2. If two parameters are given, range starts at the first and ends just before the second, incrementing by one. For example, range(2, 5)
produces 2, 3, 4. If range
is given 3 parameters, it starts at the first one, ends just before the second one, and increments by the third one. For example, range(3, 10, 2)
produces 3, 5, 7, 9. Using range
, write a loop that prints the first 3 natural numbers:
1
2
3
2.
Given the following loop, how many times is print
called? 3, 4, 5, or 6 times?
word = 'oxygen'
for char in word:
print(char)
3.
Exponentiation is built into Python and is done with **
.
print(5 ** 3)
Can you calculate 5 to the power 3 using multiplication (*
) and loops instead of the **
operator?
Feedback
Tell us at least one good thing and something we could improve by leaving feedback as post-its on the interactive board.
Good things
Things to improve
- At the very start of the day, the instructions to download the data and start a jupyter notebook is very simple, but the instructor went way too fast. I suspect that if we had gone a little slower, fewer people would have needed to go into breakout rooms. <- Thanks, we’ll take that on board.
Python - day 2 - Tom Dowrick
Notes
Use a for-loop to convert the string “hello” into a list of letters:
string = "hello"
['h', 'e', 'l', 'l', 'o']
my_list = []
Exercise 2
Plot the difference between the average inflammations reported in the first and second datasets (stored in inflammation-01.csv and inflammation-02.csv, correspondingly), i.e., the difference between the leftmost plots of the first two figures.
Don’t need to use for loops.
Exercise 3
if 4 > 5:
print('A')
elif 4 == 5:
print('B')
elif 4 < 5:
print('C')
What would be printed if you run this code?
Exercise 4
Write a function that takes a string, and returns the first and last character.
string = "hello"
def my_func(string):
return something
x = my_func(string)
Exercise 5
Write a command line application that takes a string and two numbers, and outputs those two characters from the string.
e.g. hello 1 3
hl
Questions about Python 2/2
- Example question
- student_a Example answer
- TA_1 Example answer
Feedback
Tell us at least one good thing and something we could improve by leaving feedback as post-its on the interactive board.
RSLondon Software Carpentry 2020-11-25/27
General information
This collaborative document (hackmd) is synchronized as you type, so that everyone viewing this page sees the same text. This allows you to collaborate seamlessly on documents.
Users are expected to follow our code of conduct.
The Workshop webpage contains the installations instructions for the software that we will use during the next three days.
Zoom links for each day will be shared via e-mail.
Guides to tools we will use
Pre-workshop Questionnaire
Sessions
The Unix Command line - Chris Cave-Ayland
Notes (for everyone to add them together 😉)
ls
shows the filesls -F
shows also a trailing slash for the directories (useful if you’ve haven’t got them already in a different colour).-F
is an “option”.ls -F /
shows what’s under the/
directory.ls --help
shows us how to use it! (a bit cryptic though)[OPTION]...
You can provide options,[]
means they are optional, the `[FILE]...
we can ask for a location or many as we want and they are optional.-a
=--all
man ls
is similar to the help output. You need to press q to exit. The output is clear out from the screen.-s
and-S
are different things.pwd
print working directory; first/
in the output is called the root directory, the base of all the directories.cd Desktop
moves your location to the Desktop directory~
is a short for/home/username
, your HomePaths:
/home/user/Desktop/data-shell
data-shell
..
: refers to the parent directory.
: refers to the current directorynano
open a text editor within the shell. ctrl+x to exit.cp
copies (and renames if needed) something into somewherecp quotes.txt thesis/quotations.txt
cp -r
to copy a directoryrm
to delete (remove) files. there’s not a recycle bin !! so be carefulrm -r
will allow you to delete directoriesrm -i
will ask you for confirmation before deleting stuff.wc
count characters, words and lines>
is use to redirect the output of a command into a filecat
to display files on the screensort
to sort a file,-n
to sort numerically.head
shows the first few lines of the file,-n 1
will only show the first line.tail
likehead
but for the end of the file|
(pipe) is use to redirect the output of one command as input for the next (wc -l *pdb | sort -n | head -n 1
will give the file with the smaller number of lines named...pdb
)for loops, to repeat an action over multiple files.
$ for filename in basilisk.dat minotaur.dat unicorn.dat > do > head -n 2 $filename | tail -n 1 > done
scripts, files that we put the commands we want to run in sequence. Normally called as
some_meaningful_name.sh
.$@
within the script, it will read all the arguments you pass from the command line.example:
for filename in "$@" do head -n 15 "$filename" | tail -n 5 done
history
shows you all the commands you’ve run.tab will try to autocomplete the files and directory names.
ctrl+c is used to cancel what you are running.
Questions about Unix command line
Here you can post any question you have while we are going through this document. Please, use a new bullet point for each question and sub-bullet points for their answers.
For example writing like this:
produces the following result:
Example question
What’s the difference between git bash and git cmd?
Is the sorting (when using
ls
) displayed in vertical or horizontal order?What if I wanted only the name itself without the first bit of the line (CLASSIFICATION:)?
creatures
directory:for filename in *.dat; do head -n 2 $filename | tail -n 1; done
As highlighted in the Zoom chat, this is beyond the scope of today’s session so we haven’t covered the additional commands that can help with this. As with many command line tasks, there are a few different ways to achieve this. Some options might be the use of the
cut
command orawk
command.awk
can be particularly confusing to work with but it’s worth checking out the man page nonetheless.cut
,-d
for delimeter and-f
for the field you want to show:head "$1" -n 2 | tail -n 1 | cut -d':' -f2
Feedback
Tell us at least one good thing and something we could improve by leaving feedback as post-its on the interactive board.
Version Control with Git - Iain Barrass
Notes
Software Carpentry lesson material for GitHub Remotes.
Questions about Git
Feedback
Tell us at least one good thing and something we could improve by leaving feedback as post-its on the interactive board.
Python - day 1 - Iain Stenson
Notes
Download python-novice-inflammation-data.zip.
Questions about Python 1/2
Exercises
Exercise Session 1
You can find tips or answers to most exercises in the lesson notes.
1.
What values do the variables mass and age have after each of the following statements? Test your answer by executing the lines.
mass = 47.5 age = 122 mass = mass * 2.0 age = age - 20
2.
What does the following program print out?
first, second = 'Grace', 'Hopper' third, fourth = second, first print(third, fourth)
Exercise Session 2
A section of an array is called a slice. We can take slices of character strings as well:
element = 'oxygen' print('first three characters:', element[0:3]) print('last three characters:', element[3:6])
Exercise Session 3
Why do all of our plots stop just short of the upper end of our graph? Can we adjust the y limit of our graph so that it looks better?
Can you create your own graph of the standard deviation using
Exercise Session 4
1.
Python has a built-in function called
range
that generates a sequence of numbers.range
can accept 1, 2, or 3 parameters. If one parameter is given, range generates a sequence of that length, starting at zero and incrementing by 1. For example,range(3)
produces the numbers 0, 1, 2. If two parameters are given, range starts at the first and ends just before the second, incrementing by one. For example,range(2, 5)
produces 2, 3, 4. Ifrange
is given 3 parameters, it starts at the first one, ends just before the second one, and increments by the third one. For example,range(3, 10, 2)
produces 3, 5, 7, 9. Usingrange
, write a loop that prints the first 3 natural numbers:1 2 3
2.
Given the following loop, how many times is
print
called? 3, 4, 5, or 6 times?word = 'oxygen' for char in word: print(char)
3.
Exponentiation is built into Python and is done with
**
.print(5 ** 3) # prints 125
Can you calculate 5 to the power 3 using multiplication (
*
) and loops instead of the**
operator?Feedback
Tell us at least one good thing and something we could improve by leaving feedback as post-its on the interactive board.
Good things
Things to improve
Python - day 2 - Tom Dowrick
Notes
Use a for-loop to convert the string “hello” into a list of letters:
Exercise 2
Plot the difference between the average inflammations reported in the first and second datasets (stored in inflammation-01.csv and inflammation-02.csv, correspondingly), i.e., the difference between the leftmost plots of the first two figures.
Don’t need to use for loops.
Exercise 3
What would be printed if you run this code?
Exercise 4
Write a function that takes a string, and returns the first and last character.
Exercise 5
Write a command line application that takes a string and two numbers, and outputs those two characters from the string.
e.g. hello 1 3
hl
Questions about Python 2/2
Feedback
Tell us at least one good thing and something we could improve by leaving feedback as post-its on the interactive board.
tags:
swc
teaching
live-notes